//
//  ZDepartmentsViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/2/24.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "ZDepartmentsViewController.h"
#import "ZDepartmentsFlowLayout.h"
#import "ZDepartmentsCollectionViewCell.h"
#import "ZOfficesModel.h"
static NSString * const ZDepartmentsVieIdentifier = @"ZDepartmentsVieIdentifier";
@interface ZDepartmentsViewController () <UICollectionViewDelegate,UICollectionViewDataSource>{
    UICollectionView * _departmentsView;
}
//@property (nonatomic,strong) NSMutableArray * dataList;
@property (nonatomic,strong) NSMutableArray <ZOfficesModel *> * modelArray;
@end

@implementation ZDepartmentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initialzeDatas];
    [self initlizeInterface];
}

- (void)initialzeDatas {
    self.modelArray = [NSMutableArray array];
}

- (void)initlizeInterface {
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.navigationItem.title = @"选择科室";
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
//    layout.dataList = self.dataList;
//    layout.estimatedItemSize = CGSizeMake(50, 50);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(10,10,10,10);
//    layout
    _departmentsView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _departmentsView.delegate = self;
    _departmentsView.dataSource = self;
    _departmentsView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [_departmentsView registerClass:[ZDepartmentsCollectionViewCell class] forCellWithReuseIdentifier:ZDepartmentsVieIdentifier];
    [self.view addSubview:_departmentsView];
    [_departmentsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
//    _departmentsView.mj_header = [MJRefreshNormalHeader  headerWithRefreshingBlock:^{
//        [_departmentsView.mj_header endRefreshing];
//        [self ZDNetWorking];
//    }];
//    [_departmentsView.mj_header beginRefreshing];
    [self ZDNetWorking];
}

// 网络请求 科室
- (void)ZDNetWorking {
    ESWeak(self,zdWeakSelf);
    [YJProgressHUD showProgress:@"加载中" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:nil paramarsSite:@"Index/upOffice" sucessBlock:^(id content) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"----%@",dict);
        if ([dict[@"code"] intValue] ==1) {
            [YJProgressHUD showMessage:@"加载成功" inView:self.view];
            [zdWeakSelf handleDataWith:dict];
        }else {
            [YJProgressHUD showMessage:@"加载失败" inView:self.view];
        }
        
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络请求错误" inView:zdWeakSelf.view];
    }];
}
// 处理数据
- (void)handleDataWith:(NSDictionary *)dict {
    NSArray * listArray = dict[@"list"];
    [self.modelArray removeAllObjects];
    for (NSDictionary * dict in listArray) {
        ZOfficesModel * model =  [[ZOfficesModel alloc] initWithDict:dict];
        [self.modelArray addObject:model];
    }
    [_departmentsView reloadData];
    
}

//集合视图协议
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

//每组单元格个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    if(self.modelArray.count) {
//        return self.modelArray.count;
//    }else {
//        return 0;
//    }
    return self.modelArray.count ? self.modelArray.count : 0;
    
}
// 单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZDepartmentsCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:ZDepartmentsVieIdentifier forIndexPath:indexPath];
    cell.model = self.modelArray[indexPath.row];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGSize size = CGSizeMake(self.modelArray[indexPath.row].oWidth,40*xsIZE);
    return size;
}
//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//    CGSize size={0,0};
//    return size;
//}
// 选择科室
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.ZDeparDelegate && [self.ZDeparDelegate conformsToProtocol:@protocol(ZDeparDelegate)]) {
        
        NSString * keshi = self.modelArray[indexPath.row].title;
        NSString * kid = self.modelArray[indexPath.row].offID;
        if (self.ZDeparDelegate && [self.ZDeparDelegate conformsToProtocol:@protocol(ZDeparDelegate)]) {
            [self.ZDeparDelegate ZDepartmentWithString:keshi withID:kid];
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}




@end
